package com.health.controller.manage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.common.PageBean;
import com.health.common.QueryParam;
import com.health.common.R;
import com.health.pojo.CheckItem;
import com.health.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查项控制层，负责处理和检查项相关的所有请求
 */
@RestController
@RequiredArgsConstructor //构造器注入
@RequestMapping("/api/manage/checkItem")
@Api(tags = "检查项管理")
@Slf4j
public class CheckItemController {

    private final CheckItemService checkItemService;

    /**
     * 分页条件查询
     *
     * @param queryParam 查询参数对象，包含当前页码和页面大小以及查询字符串
     * @return 返回分页查询结果，包括检查项列表和总记录数
     */
    @ApiOperation(value = "分页条件查询")
    @GetMapping("/pageQuery")
    public R pageQuery(QueryParam queryParam) {
        log.info("分页条件查询 {}  ", queryParam);
        //构建Page分页对象
        Page<CheckItem> page = new Page<>(queryParam.getCurrentPage(), queryParam.getPageSize());

        //执行分页查询，并根据查询字符串进行模糊匹配
        checkItemService.query()
                .like(queryParam.getQueryString() != null, "name", queryParam.getQueryString())
                .page(page);

        //创建PageBean对象
        PageBean pageBean = new PageBean(page.getRecords(), page.getTotal());

        return R.success(pageBean);
    }


    /**
     * 添加检查项
     *
     * @param checkItem
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加检查项")
    public R addCheckItem(@RequestBody CheckItem checkItem) {
        log.info("添加检查项 {}  ", checkItem);
        checkItemService.save(checkItem);
        return R.success();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public R getById(@PathVariable Integer id) {
        log.info("根据id查询 {}  ", id);
        CheckItem checkItem = checkItemService.getById(id);
        return R.success(checkItem);
    }

    /**
     * 修改检查项
     *
     * @param checkItem
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改检查项")
    public R updateCheckItem(@RequestBody CheckItem checkItem) {
        log.info("修改检查项 {}  ", checkItem);
        checkItemService.updateById(checkItem);
        return R.success();
    }

    /**
     * 删除检查项
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public R deleteCheckItem(@PathVariable Integer id) {
        log.info("根据id删除 {}  ", id);
        checkItemService.deleteCheckItem(id);
        return R.success();
    }


    /**
     * 查询所有检查项
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有")
    public R getCheckItemList() {
        log.info("查询所有");
        // List<CheckItem> list = checkItemService.list();
        List<CheckItem> list = checkItemService.query().list();
        return R.success(list);
    }
}
